<?php

namespace common\models\notice;

use common\helpers\ArrayHelper;
use Yii;

/**
 * This is the model class for table "{{%showbiz_role}}".
 *
 * @property int $id
 * @property int $parent_id 父级分类ID
 * @property string $name 名称
 * @property string $name_en 英文名称
 * @property string $description 描述
 * @property string $thumb 缩略图
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class ShowbizRole extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%showbiz_role}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['parent_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'required'],
            [['name', 'name_en'], 'string', 'max' => 100],
            [['description', 'thumb'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'parent_id' => '父级分类ID',
            'name' => '名称',
            'name_en' => '英文名称',
            'description' => '备注或描述',
            'thumb' => '缩略图',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getNoticeNeeds() {
        return $this->hasMany(NoticeNeed::class, ['role_id' => 'id']);
    }

    public function getNotices() {
        return $this->hasMany(Notice::class, ['id' => 'notice_id'])->via('noticeNeeds');
    }

    public static function findActiveList() {
        return ArrayHelper::map(static::find()->active()->all(), 'id', 'name');
    }

    public function fields() {
        return [
            'id',
            'name'
        ];
    }

}
